import VueRouter from 'vue-router'
import store from './store'
import Vue from 'vue'

/* 首页路由 */
const Login = ()=> import(/* webpackChunkName: "Login" */ './component/login.vue');

/* 首页路由 */
const Index = ()=> import(/* webpackChunkName: "Index" */ './component/index.vue');

const Deployment = () => import(/* webpackChunkName: "Deployment" */ './component/deployment/deployment.vue');

const DeploymentList = ()=> import(/* webpackChunkName: "DeploymentList" */ './component/deployment/deployment.list.vue');

const ProcessDefinitionsList = () => import(/* webpackChunkName: ProcessDefinitionsList" */'./component/process/process.definitions.list.vue');

const ModelList = () => import(/* webpackChunkName: ModelList" */'./component/model/model.list.vue');

const TaskList = () => import(/* webpackChunkName: TaskList" */'./component/task/task.list.vue');

const ExecutionList = () => import(/* webpackChunkName: ExecutionList" */'./component/execution/execution.list.vue');

const UserList = () => import(/* webpackChunkName: UserList" */'./component/user/user.list.vue');

const UserGroupList = () => import(/* webpackChunkName: UserGroupList" */'./component/user/user.group.list.vue');

const ManagementDatabase = () => import(/* webpackChunkName: "ManagementDatabase" */'./component/management/management.database.vue');

const ManagementEngine = () => import(/* webpackChunkName: "ManagementDatabase" */'./component/management/management.engine.vue');

const ManagementProperties = () => import(/* webpackChunkName: "ManagementProperties" */'./component/management/management.properties.vue');

Vue.use(VueRouter);

const router:VueRouter = new VueRouter({
    routes:[
        {
            path:"/login",
            component: Login
        },
        {
            path:"/",
            component: Index,
            meta: {
                requireAuth: true,
            },
            children:[
                {
                    path:"deployment",
                    component: Deployment,
                    children:[
                        {
                            path:"",
                            component: DeploymentList
                        }
                    ]
                },
                {
                    path:"process",
                    component: ProcessDefinitionsList
                },
                {
                    path:"model",
                    component: ModelList
                },
                {
                    path:"task",
                    component: TaskList
                },
                {
                    path:"execution",
                    component: ExecutionList
                },
                {
                    path:"user",
                    component: UserList
                },
                {
                    path:"user/group",
                    component: UserGroupList
                },
                {
                    path:"management/database",
                    component:ManagementDatabase
                },
                {
                    path:"management/engine",
                    component:ManagementEngine
                },
                {
                    path:"management/properties",
                    component:ManagementProperties
                }
            ]
        }
    ]
});

/* 路由拦截器，保证跳转路由时token的存在 */
router.beforeEach((to, from, next) => {

    console.log("路由跳转："+from.path+" 到 "+to.path);

    /* 如果路由需要认证 */
    if(to.meta.requireAuth){
        /* token不存在时，转向登录页面 */
        if(!store.getters.token){
            console.log("当前路由需要登陆");
            next("/login");
            return
        }
    }
    next();
});

export default router;